package com.sc.VenusQueryBeta.ui.fragments.home.child;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.hengjun.kingstar.sdk.adapter.FragmentAdapter;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.fragment.BaseMVPCompatFragment;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.constant.TabFragmentIndex;
import com.sc.VenusQueryBeta.contract.home.HomeMainContract;
import com.sc.VenusQueryBeta.presenter.home.HomeMainPresenter;
import com.sc.VenusQueryBeta.ui.activites.search.SearchRootActivity;
import com.sc.VenusQueryBeta.ui.fragments.home.child.tabs.GuiBinFragment;
import com.sc.VenusQueryBeta.ui.fragments.home.child.tabs.YuDingFragment;
import com.sc.VenusQueryBeta.ui.fragments.home.child.tabs.ZhiXunFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by SC-02 on 2018/9/19.
 * 子HomeFragment
 * 1继承BaseMVPCompatFragment
 * 2必须实现契约类里面的view接口并且重写对应的方法（该fragment有请求就必须实现，重写，不然会报错）
 * 3重写onLazyInitView
 */

public class HomeFragment extends BaseMVPCompatFragment<HomeMainContract.HomeMainPresenter> implements HomeMainContract.IHomeMainView {

    @BindView(R.id.tl_tabs)
    TabLayout tlTabs;
//    @BindView(R.id.app_bar)
//    AppBarLayout appBar;
    @BindView(R.id.vp_fragment)
    ViewPager vpFragment;
    @BindView(R.id.home_container)
    CoordinatorLayout homeContainer;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    Unbinder unbinder1;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private List<Fragment> fragments;
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;
    /**
     * 提供实例化对象
     *
     * @return
     */
    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.getTabList();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        fragments = new ArrayList<>();
    }

    @Override
    public void onResume() {
        super.onResume();


    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return HomeMainPresenter.newInstance();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_child;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        view.setBackgroundColor(Color.rgb(255,255,255));
    }

    @Override
    public void showTabList(String[] tabs) {
        for (int i = 0; i < tabs.length; i++) {
            tlTabs.addTab(tlTabs.newTab().setText(tabs[i]));
            switch (i) {
                case TabFragmentIndex.TAB_ZHIXUN_INDEX://最新资讯
                    fragments.add(ZhiXunFragment.newInstance());
                    break;
                case TabFragmentIndex.TAB_GUIBIN_INDEX://贵宾厅
                    fragments.add(GuiBinFragment.newInstance());
                    break;
                case TabFragmentIndex.YUDING://预定服务
                    fragments.add(YuDingFragment.newInstance());
                    break;
                default:
                    fragments.add(ZhiXunFragment.newInstance());
                    break;
            }
        }
        vpFragment.setAdapter(new FragmentAdapter(getChildFragmentManager(), fragments));
        vpFragment.setCurrentItem(TabFragmentIndex.TAB_ZHIXUN_INDEX);//要设置到viewpager.setAdapter后才起作用
        tlTabs.setupWithViewPager(vpFragment);
        tlTabs.setVerticalScrollbarPosition(TabFragmentIndex.TAB_ZHIXUN_INDEX);
        //tlTabs.setupWithViewPager方法内部会remove所有的tabs，这里重新设置一遍tabs的text，否则tabs的text不显示
        for (int i = 0; i < tabs.length; i++) {
            tlTabs.getTabAt(i).setText(tabs[i]);
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
//            mActivity.setIsTransAnim(false);
            mActivity.finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
//                ToastUtils.showToast(R.string.press_again);
            ToastUtils.showToast("再按一次退出");
        }
        return true;
    }

    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        startNewActivity(SearchRootActivity.class);
    }

}
